package com.fesine.ssh.dao.impl;

import com.fesine.ssh.dao.CategoryDao;
import com.fesine.ssh.model.Category;
import com.fesine.ssh.util.GenerateUUID;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Fesine on 2016/2/21.
 */
@Repository("categoryDao")
public class CategoryDaoImpl implements CategoryDao {
    @Autowired
    private SessionFactory sessionFactory;

    private Session getCurrentSession() {
        return this.sessionFactory.getCurrentSession();
    }

    @Override
    public Category load(String id) {
        return (Category)this.getCurrentSession().load(Category.class, id);
    }

    @Override
    public Category get(String id) {
        return (Category) this.getCurrentSession().get(Category.class, id);
    }

    @Override
    public List<Category> findAll() {
        String hql = String.format("from Category order by cateId");
        List<Category> categoryList =
                this.getCurrentSession().createQuery(hql).setCacheable(true).list();
        return categoryList;
    }

    @Override
    public List<Category> findAll(int offset, int pageSize) {
        String hql = String.format("from Category order by cateId");
        Query query = this.getCurrentSession().createQuery(hql).setCacheable(true);
        query.setFirstResult(offset);
        query.setMaxResults(pageSize);
        List<Category> categoryList = query.list();
        return categoryList;
    }

    /**
     * 支持模糊查询,如果有cateId字段则精确查询
     * @param category
     * @return
     */
    @Override
    public List<Category> findByEntity(Category category) {
        StringBuffer hql = new StringBuffer("from Category ");
        if (null != category.getParent()) {
            hql.append("and parent =" + category.getParent() + " ");
        }
        if (0 != category.getCateId()) {
            hql.append("and cateId =" + category.getCateId() + " ");
        }
        if (null != category.getText()) {
            hql.append("and text like '%" + category.getText() + "%' ");
        }
        if (null != category.getDescription()) {
            hql.append("and text like '%" + category.getDescription() + "%' ");
        }
        if (null !=category.getLeaf()) {
            hql.append("and leaf="+ category.getLeaf());
        }
        hql.append(" order by cateId");
        List<Category> categoryList =
                this.getCurrentSession().createQuery(hql.toString().replaceFirst("and","where")).setCacheable(true).list();
        return categoryList;
    }

    @Override
    public List<Category> findByEntity(Category entity, int offset, int pageSize) {
        return null;
    }

    @Override
    public void persist(Category entity) {
        this.getCurrentSession().persist(entity);
    }

    @Override
    public String save(Category entity) {
        entity.setId(GenerateUUID.getUUId());
        return (String) this.getCurrentSession().save(entity);
    }

    @Override
    public void update(Category entity) {
        this.getCurrentSession().update(entity);
    }

    @Override
    public void saveOrUpdate(Category entity) {
        this.getCurrentSession().saveOrUpdate(entity);
    }

    @Override
    public void delete(String id) {
        Category entity = this.load(id);
        this.getCurrentSession().delete(entity);

    }

    @Override
    public void flush() {
        this.getCurrentSession().flush();
    }

    @Override
    public List<Category> findByParent(int pid) {
        String hql = String.format("from Category where parent='%s' order by parent",pid);
        List<Category> categoryList =
                this.getCurrentSession().createQuery(hql).setCacheable(true).list();
        return categoryList;
    }

    @Override
    public List<Category> findTopAll() {
        String hql = String.format("from Category where parent=0 order by cateId");
        List<Category> categoryList =
                this.getCurrentSession().createQuery(hql).setCacheable(true).list();
        return categoryList;
    }
}
